package com.wangshuo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wangshuo.common.JwtHelper;
import com.wangshuo.config.Result;
import com.wangshuo.entity.Orderr;
import com.wangshuo.entity.OrderrItem;
import com.wangshuo.service.OrderrService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/order")
public class OrderrController {
  @Resource
  private JwtHelper jwtHelper;
  @Resource
  private OrderrService orderrService;
    @PostMapping("/add")
    public Result add(@RequestBody Orderr order, HttpServletRequest httpServletRequest){
        Long uid = jwtHelper.getUserId(httpServletRequest.getHeader("token"));

        order.setZt("待支付");
        order.setXdtime(LocalDateTime.now());
        order.setOrdernum(((UUID.randomUUID().toString())));
        order.setUserid(Math.toIntExact(uid));

        orderrService.saveOrUpdate(order);
        return Result.ok("成功");
    }
    @GetMapping("/list")
    public Result list(HttpServletRequest httpServletRequest){
        Long uid = jwtHelper.getUserId(httpServletRequest.getHeader("token"));
//        LambdaQueryWrapper<Orderr> lambdaQueryWrapper=new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.eq(Orderr::getUserid,uid);
        List<OrderrItem> list=orderrService.getlist(uid);
        return Result.ok(list);

    }
    @GetMapping("/slist")
    public Result sliist(HttpServletRequest httpServletRequest){
        Long uid = jwtHelper.getUserId(httpServletRequest.getHeader("token"));
//        LambdaQueryWrapper<Orderr> lambdaQueryWrapper=new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.eq(Orderr::getUserid,uid);
        List<OrderrItem> list=orderrService.getlist1(uid);
        return Result.ok(list);

    }
    @PostMapping("/quxiao")
    public Result quxiao(@RequestBody Orderr orderr){
        orderrService.saveOrUpdate(orderr);
return Result.ok("成功");
    }
    @PostMapping("/zhifu")
    public Result zhifu(@RequestBody Orderr orderr){
        orderr.setZfdh(UUID.randomUUID().toString());
        orderr.setZftime(LocalDateTime.now());
        orderrService.saveOrUpdate(orderr);
        return Result.ok("成功");
    }
    @GetMapping("/alllist")
    public Result alllist(@RequestParam Integer pageNum,@RequestParam Integer pageSize,@RequestParam String keyword){
        IPage<OrderrItem> page=new Page<>(pageNum,pageSize);
        IPage<OrderrItem> list=orderrService.getlistpage(page,keyword);
        return Result.ok(list);
    }
    @GetMapping("/delete")
    public Result delete(@RequestParam long id){
        orderrService.removeById(id);
        return Result.ok("成功");
    }
}
